# -*- coding: utf-8 -*-
"""
@Env 
@Time 2024/9/3 上午10:54
@Author yzpang
@Function: 
"""
import traceback
from ..entity.response import BaseResponse
from ..entity.request import TrainMessage
from ..service import train_service
from modelserver.configs.base_config import get_logger


def mount_train_routes(app):
    """
    挂载训练相关接口
    :param app:
    """
    app.post(path="/train/start",
             tags=['Model Train Start'],
             summary="启动训练",
             )(train_start)

    app.post(path="/train/stop",
             tags=['Model Train Stop'],
             summary="停止训练",
             )(train_stop)

    app.post(path="/train/list",
             tags=['Model Train List'],
             summary="训练列表",
             )(train_list)


def train_start(request: TrainMessage) -> BaseResponse:
    """启动训练"""
    get_logger().info(request)
    try:
        result = train_service.train_start(request)
        return BaseResponse(data=result)
    except Exception as e:
        traceback.print_exc()
        return BaseResponse(code=500, msg="failed", data=str(e))


def train_stop(request: TrainMessage) -> BaseResponse:
    """停止训练"""
    get_logger().info(request)
    try:
        result = train_service.train_stop(request)
        return BaseResponse(data=result)
    except Exception as e:
        traceback.print_exc()
        return BaseResponse(code=500, msg="failed", data=str(e))


def train_list() -> BaseResponse:
    """训练列表"""
    try:
        result = train_service.train_list()
        return BaseResponse(data=result)
    except Exception as e:
        traceback.print_exc()
        return BaseResponse(code=500, msg="failed", data=str(e))

